﻿using System;
using System.Linq;

namespace AIC_Notification
{
    public class EssayPendingReportForAM : EssayPendingReportForAC
    {
        private int City;
        private string CityName;

        public EssayPendingReportForAM(bool ShowDetail = false)
        {
            ReportTitle = "Draft Pending Revision (Office)";
            ShowWarning = ShowDetail;
            ShowStudentSection = false;
        }

        public override bool ShowDraftRow(EssayStatistics EssayStats)
        {
            bool DisplayRow = false;

            if (EssayStats.HasOverDuedDraft)
                DisplayRow = true;
            else if (EssayStats.MissingDueDate)
                DisplayRow = ShowWarning;
            else
                DisplayRow = false;

            return DisplayRow;
        }

        public override string InsertTeachersName(SOF_Application student)
        {
            string tableBody = "";

            tableBody += "<td><div>";
            tableBody += String.Format("<div><div style=\"font-size: {1}px; {2}\">PT: {0}</div></div>", student.AC_PT, 11, "color: red");
            if (student.AC_ST != "")
                tableBody += String.Format("<div><div style=\"font-size: {1}px; {2}\">ST: {0}</div></div>", student.AC_ST, 11, "color: red");
            tableBody += String.Format("<div><div style=\"font-size: {1}px; {2}\">EC: {0}</div></div>", student.EC, 11, "");
            tableBody += "</div></td>";

            return tableBody;
        }

        private void AddAcademicManagerEmail(string AM)
        {
            Teacher AcademicManager = null;

            AcademicManager = TeacherDB.GetTeacherByName(AM);
            if (AcademicManager != null)
                toRecipients.Add(AcademicManager.Email);
            else
                Global.SysUtil.Log(String.Format("Academic Manager \"{0}\" not found", AM), 2);
        }

        public override void SendReportAsEmail()
        {
            for (int i = 0; i < CityDB.NoOfCities; i++)
            {
                // Only for office with 1 or more student
                if (CityDB.Cities[i].Students.Count() > 0)
                {
                    string HTMLBody;

                    City = i;
                    CityName = CityDB.GetCityName((CityDB.CityEnum) i);

                    Students = CityDB.Cities[i].Students;
                    HeaderInformation = CityName;
/*
                    if ((((CityDB.CityEnum)i == CityDB.CityEnum.Hangzhou) || (CityDB.CityEnum)i == CityDB.CityEnum.Chengdu))
                    {
                        DisplayPendingDays = 1;
                    }
                    else
                    {
*/
                        DisplayPendingDays = 2;
                    //                    }

                    FileName = String.Format("{0}-{1}-{2:yyyyMMdd-HHmmss}.htm", ReportTitle, CityName, Global.Now());
                    Global.SysUtil.Log("Generating Report: " + FileName);

                    HTMLBody = GetHTMLBody();

                    if (IsEmptyBody == true) // only send e-mail or generate content if any
                        continue;

                    // Save Report to File
                    SaveReportToFile(FileName, HTMLBody);

                    // Send Report E-mail
                    toRecipients.Clear();
                    ccRecipients.Clear();

                    string[] AcademicManagers = TeacherDB.GetAcademicManager((CityDB.CityEnum)i);
                    foreach (var AcademicManager in AcademicManagers)
                        AddAcademicManagerEmail(AcademicManager);

                    string[] SystemAdmins = TeacherDB.GetSystemAdmin();
                    foreach (var SystemAdmin in SystemAdmins)
                        ccRecipients.Add(SystemAdmin);

                    ccRecipients.Add("wai.cheng@jq-edu.com");

                    SubjectID = CityName;
                    Global.SysUtil.SendHTML(toRecipients, ccRecipients, GetSubject(), HTMLBody);
                }
            }
        }
    }
}
